# Copyright (c) 2024 Cooper0@qq.com

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# 输出所有的警告
add_compile_options("-Wall")

# 将警告当做错误输出
add_compile_options("-Werror")

# 检查printf等格式化函数的调用
add_compile_options("-Wformat")

# 检查函数返回值
add_compile_options("-Wreturn-type")

# 检查未使用的定义
add_compile_options("-Wunused")

# 检查代码不可达
add_compile_options("-Wunreachable-code")

# 检查没有初始化被使用
add_compile_options("-Wuninitialized")

# 有符号和无符号数比较
add_compile_options("-Wsign-compare")

# 符号转换
add_compile_options("-Wsign-conversion")

# 栈溢出检测，所有函数
add_compile_options("-fstack-protector-all")
# add_compile_options("-fstack-protector-strong") # 二选一

# 在动态链接表中添加RELRO段，防止动态链接表攻击
add_compile_options("-Wl,-z,relro")

# 立即绑定
add_compile_options("-Wl,-z,now")

# 堆栈不可执行
add_compile_options("-Wl,-z,noexecstack")

# 影藏符号
add_compile_options("-fvisibility=hidden")

# 检查函数原型, 只在c语言中有效
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wstrict-prototypes")

# mcheck 需要以debug模式编译
option(ENABLE_MCHECK "User mcheck" OFF)
if(ENABLE_MCHECK AND ENABLE_DEBUG)
    add_compile_options("-include mcheck.h")
endif()

# 添加日志头文件
option(ENABLE_TOOL_LOG "add include dir tool/log/" OFF)
if (ENABLE_TOOL_LOG)
    add_compile_options("-I${WORK_TOP}/tool/log")
endif()

# 添加cjson头文件搜索路径
if(ENABLE_CJSON)
    add_compile_options("-I${WORK_TOP}/third_party/cjson/cJSON")
endif()
